package com.harmontronics.hanma.paas.paas.domain.app.service.dialect;

import com.harmontronics.hanma.paas.paas.domain.app.entity.valueobject.ViewDataBaseTypeEnum;
import com.harmontronics.hanma.paas.paas.domain.app.repository.persistence.HmDatasourceRepository;
import com.harmontronics.hanma.paas.paas.domain.app.repository.po.HmDatasourcePO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author ldb
 */
@Service
public class DialectDecision {

    @Autowired
    private HmDatasourceRepository hmDatasourceRepository;

    @Autowired
    private OracleDialectService oracleDialectService;

    @Autowired
    private MysqlDialectService mysqlDialectService;

    @Autowired
    private PostgresDialectService postgresDialectService;

    @Autowired
    private SqlServerDialectService sqlServerDialectService;

    public AbstractDialectService getService(Long datasourceId) {
        HmDatasourcePO datasourcePO = hmDatasourceRepository.getById(datasourceId);
        if (datasourcePO.getType() == ViewDataBaseTypeEnum.POSTGRES) {
            return postgresDialectService;
        }
        if (datasourcePO.getType() == ViewDataBaseTypeEnum.ORACLE) {
            return oracleDialectService;
        }

        if (datasourcePO.getType() == ViewDataBaseTypeEnum.MYSQL) {
            return mysqlDialectService;
        }

        if (datasourcePO.getType() == ViewDataBaseTypeEnum.MSSQL) {
            return sqlServerDialectService;
        }

        return null;
    }

}
